#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "fcntl.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"

#include "pub_printf.h"

static char usrdata[] = {"usr data!"};
/**
  * @brief  提示信息打印
  * @note   
  * @param  
  * @retval 
  */
void usage_info(void)
{
	printf("\n");
	printf(F_GREEN"+++++++++++++++++++++++++++++++++++++++++\n"CLS_ALL);
	printf(F_GREEN"+       help information  @ssq          +\n"CLS_ALL);
	printf(F_GREEN"+++++++++++++++++++++++++++++++++++++++++\n"CLS_ALL);

	printf(F_YELLOW"help:\n"CLS_ALL);
	printf("use format: ./app_name /dev/device_name arg1 ... \n");
	printf("\n");

	printf(F_YELLOW"command info:\n"CLS_ALL);
	printf("  (1)load module  : insmod module_name.ko\n");
	printf("  (2)unload module: rmmod module_name.ko\n");
	printf("  (3)show module  : lsmod\n");
	printf("  (4)view device  : cat /proc/devices\n");
	printf("  (5)create device node: mknod /dev/device_name c major_num secondary_num \n");
	printf("  (6)show device node  : ls /dev/device_name \n");
	printf(F_GREEN"+++++++++++++++++++++++++++++++++++++++++\n"CLS_ALL);
}
/**
  * @brief  main主程序
  * @note   
  * @param  argc 数组元素个数
  * @param  argv 具体参数
  * @retval 0 成功;其他 失败
  */
int main(int argc, char *argv[])
{
	int fd, retvalue;
	char *filename;
	char readbuf[100], writebuf[100];

	if(argc != 3){
		usage_info();
		return -1;
	}

	filename = argv[1];

	/* 打开驱动文件 */
	fd  = open(filename, O_RDWR);
	if(fd < 0){
		PRTE("Can't open file %s\r\n", filename);
		return -1;
	}

	if(atoi(argv[2]) == 1){ /* 从驱动文件读取数据 */
		retvalue = read(fd, readbuf, 50);
		if(retvalue < 0){
			PRT("read file %s failed!\r\n", filename);
		}else{
			/*  读取成功，打印出读取成功的数据 */
			PRT("read data:%s\r\n",readbuf);
		}
	}

	if(atoi(argv[2]) == 2){
 	/* 向设备驱动写数据 */
		memcpy(writebuf, usrdata, sizeof(usrdata));
		retvalue = write(fd, writebuf, 50);
		if(retvalue < 0){
			PRTE("write file %s failed!\r\n", filename);
		}
	}

	/* 关闭设备 */
	retvalue = close(fd);
	if(retvalue < 0){
		PRTE("Can't close file %s\r\n", filename);
		return -1;
	}

	return 0;
}



